package com.wdl.modules.myhouse.controller;


import com.wdl.modules.myhouse.vo.TestVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 昏昏的君
 */
@RestController
@RequestMapping("/test")
@Api(tags = "测试")
@Slf4j
public class TestController {


    @DeleteMapping("/test")
    public String testDel(){
        return "删除请求";
    }

    @GetMapping("/test1")
    @ApiOperation("测试连同性")
    public String test(){
        return "测试通过";
    }

    @GetMapping("/admin")
    @PreAuthorize("hasAnyRole('ADMIN')")
    @ApiOperation("管理员身份测试")
    public String admin(){
        return "你好,admin";
    }

    @GetMapping("/register")
    @ApiOperation("注册用户不需要验证身份")
    public String register(){
        return "你好,注册身份信息";
    }

    @GetMapping("/register.html")
    @PreAuthorize("hasAnyRole('ADMIN')")
    @ApiOperation("注册用户不需要验证身份")
    public String register1(){
        return "你好,注册身份信息页面";
    }



    @GetMapping("/userInfo")
    @PreAuthorize("hasAnyRole('USER')")
    @ApiOperation("获取用户信息")
    public String userInfo(@AuthenticationPrincipal UserDetails customUser, HttpServletResponse response) throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication.getPrincipal());
        System.out.println(authentication);

        response.getOutputStream().println(customUser.getUsername());
        return "你好,注册身份信息页面";
    }




    @PostMapping("/testArray")
    public void getArray(HttpServletRequest request, @RequestBody List<TestVo> vos){
        log.info(vos.toString());
    }
}
